﻿using System;
using System.Collections.Generic;
using System.Linq;
using RBOSS.Data;
using System.Data.Entity.Migrations;

namespace RBOSS.Business.Products
{
    public class ProductSubsystem : IProductSubsystem
    {
        RBOSSEntities _context = new RBOSSEntities();
        public void CreateProduct(ProductItem objProdItem)
        {
            var product = new RBOSS.Data.Product();
            product.ProductName = objProdItem.ProductName;
            product.Quantity    = objProdItem.Qunatity;
            product.UnitPrice   = objProdItem.Price;
            product.Image       = objProdItem.Image;
            product.Description = objProdItem.Description;
            product.CategoryId  = objProdItem.CategoryId;
            product.UserId      = objProdItem.UserId;
            product.IsProductActive = objProdItem.IsProductAvailable == 1 ? true : false;

            _context.Products.AddOrUpdate(product);
            _context.SaveChanges();
                       
        }

        public void UpdateProduct(ProductItem objProdItem)
        {
            
            var product = (from p in _context.Products where p.ProductId == objProdItem.ProductId select p).FirstOrDefault();
            if (product != null)
            {
                product.ProductName = objProdItem.ProductName;
                product.UnitPrice = objProdItem.Price;
                product.Quantity = objProdItem.Qunatity;
                product.Description = objProdItem.Description;
                if (product.Image.EndsWith("/"))
                {
                    product.Image = objProdItem.Image;
                }
                product.CategoryId = objProdItem.CategoryId;
                product.IsProductActive = objProdItem.IsProductAvailable == 1 ? true : false;

                _context.SaveChanges();
            }
        }

        public List<ProductItem> GetProductsByCategory(string categoryName)
        {
            var category = (from c in _context.Categories where c.CategoryName == categoryName select c).FirstOrDefault();
            if (category != null)
            {
                var products = (from p in _context.Products
                    where p.CategoryId == category.CategoryId && p.IsProductActive && p.IsProductEnabled
                    select new ProductItem
                    {
                        ProductId = p.ProductId,
                        ProductName = p.ProductName,
                        Qunatity = p.Quantity,
                        Price = p.UnitPrice,
                        Description = p.Description,
                        Image = p.Image,
                        VendorId = p.UserId,
                        CategoryId = p.CategoryId
                    }).ToList();
                return products;
            }
            return new List<ProductItem>();
        }
        public List<ProductItem> SearchProduct(string productName)
        {
            
                var products = (from p in _context.Products
                                where p.ProductName.Contains(productName) && p.IsProductEnabled && p.IsProductActive
                                select new ProductItem
                                {
                                    ProductId = p.ProductId,
                                    ProductName = p.ProductName,
                                    Qunatity = p.Quantity,
                                    Price = p.UnitPrice,
                                    Description = p.Description,
                                    Image = p.Image,
                                    VendorId = p.UserId,
                                    CategoryId = p.CategoryId
                                }).ToList();
                return products;
            
        }

        public IProduct GetProduct(int productId)
        {
            var product = (from p in _context.Products
                where p.ProductId == productId
                select new ProductItem
                {
                    ProductId = p.ProductId,
                    ProductName = p.ProductName,
                    Qunatity = p.Quantity,
                    Price = p.UnitPrice,
                    Description = p.Description,
                    Image = p.Image,
                    VendorId = p.UserId,
                    CategoryId = p.CategoryId,
                    IsProductAvailable = p.IsProductActive == true ? 1: 0
                }).FirstOrDefault();

            return product;
        }

        public List<ProductItem> GetProducts()
        {
            var products = (from p in _context.Products
                            where p.IsProductActive && p.IsProductEnabled
                            orderby p.Quantity descending 
                            select new ProductItem
                            {
                                ProductId = p.ProductId,
                                ProductName = p.ProductName,
                                Qunatity = p.Quantity,
                                Price = p.UnitPrice,
                                Description = p.Description,
                                Image = p.Image,
                                VendorId = p.UserId,
                                CategoryId = p.CategoryId
                            }).Take(20).ToList();
            return products;
        }
    }
}